package com.chexiehui.jc.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.aries.ui.view.radius.RadiusTextView;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpActivity;
import com.chexiehui.jc.mvp.mine.FeedBackCovenant;
import com.chexiehui.jc.mvp.mine.FeedBackPresenter;
import com.chexiehui.jc.utils.SoftKeyboardFixerForFullscreen;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @Description 反馈
 * @Auth SongJun
 * @Date 2019/11/28 18:48
 **/
public class FeedBackActivity extends BaseMvpActivity<FeedBackPresenter> implements FeedBackCovenant.View {

    @BindView(R.id.et_feed_content)
    EditText etFeedContent;
    @BindView(R.id.rtv_save_submit)
    RadiusTextView rtvSaveSubmit;
    @BindView(R.id.tv_introduce_count)
    TextView tvIntroduceCount;
    private int maxTextCount = 200;

    @Override
    protected int getLayout() {
        return R.layout.activity_feed_back;
    }

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("反馈");
    }

    @Override
    protected void initView(Bundle var1) {
        SoftKeyboardFixerForFullscreen.assistActivity(this);
        etFeedContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                etFeedContent.removeTextChangedListener(this);//**** 注意的地方
                if(s.length() == 0){
                    tvIntroduceCount.setText(maxTextCount + "字以内");
                }else if (s.length() > maxTextCount) {
                    etFeedContent.setText(s.toString().substring(0, maxTextCount));
                    etFeedContent.setSelection(maxTextCount);
                    tvIntroduceCount.setText(maxTextCount + "字以内");
                } else {
                    tvIntroduceCount.setText(s.length() + "/" + maxTextCount + "字");
                }
                etFeedContent.addTextChangedListener(this);//****  注意的地方
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    protected FeedBackPresenter createPresenter() {
        return new FeedBackPresenter(this);
    }

    @OnClick(R.id.rtv_save_submit)
    public void onViewClicked() {
        mvpPresenter.save();
    }

    @Override
    public void onSaveSuccess(BaseModel<Object> successResult) {
        showToast(successResult.getResultInfo());
        onBackPressed();
    }

    @Override
    public void onSaveFailure(BaseModel<Object> failureResult) {
        showToast(failureResult.getResultInfo());
    }

    @Override
    public String getFeedContent() {
        return etFeedContent.getText().toString().trim();
    }

}